#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int N=100005;
const long long INF=1000000000000000010;
int n,m,k;
int s[N],a[N];
int ans[N],t[N],pos[(1<<18)+10];
ll tot[22];
bool f[30][(1<<18)+10];
struct nd{
    bool fl;
    int r,g;
    ll sum1,sum2;
    bool tg;
}p[(1<<20)+10];
void mt(int i){
    p[i].sum1=p[i<<1].sum1+p[i<<1|1].sum1;
    p[i].sum2=p[i<<1].sum2+p[i<<1|1].sum2;
}
/*void nw(int i){
    if(p[i].sum1) p[i].d1=1;
    else p[i].d1=0;
    if(p[i].sum2) p[i].d2=1;
    else p[i].d2=0;
}*/
void bd(int i,int r,int g){
//    cout<<"!"<<i<<" "<<r<<" "<<g<<endl;
    p[i].r=r;
    p[i].g=g;
    p[i].fl=f[r][g];
    p[i].tg=0;
    if(r==1){
        pos[g]=i;
        p[i].sum2=0;
        p[i].sum1=0;
        return ;
    }
    bd(i<<1,r-1,g<<1);
    bd(i<<1|1,r-1,g<<1|1);
    mt(i);
//    nw(i);
}
ll tmp;
bool ok;
void upd(int i){
    if(p[i].r>1){
        mt(i);
//        cout<<"!"<<p[i].sum1<<" "<<p[i].sum2<<endl;
    }else{
        p[i].sum1=tmp;
        p[i].sum2=0;
    }
    if(i==1) return ;
    bool num=i&1;
    if(p[i>>1].fl==num){
        if(ok){
            if(p[i].sum1+p[i].sum2==tmp){
                if(a[tmp-1]<p[i].r){
                    ok=0;
                }
                else{
                    p[i^1].sum1=p[i^1].sum2=0;
                    p[i^1].tg=1;
                }
            }else{
                if(tmp<p[i].r) ok=0;
            }
        }
    }
    upd(i>>1);
}
void upd2(int i){
    p[i].sum1-=tmp;
    if(i>1) upd2(i>>1);
}
bool ask(int i,int l,int r,int k){
    if(p[i].tg) return 0;
    if(p[i].r==1) return 1;
    int mid=l+r>>1;
    if(k<=mid) return ask(i<<1,l,mid,k);
    else return ask(i<<1|1,mid+1,r,k);
}
void slv(){
    int x[4];
    for(int i=0;i<4;i++) cin>>x[i];
    for(int i=0;i<n;i++) a[i]=s[i]^x[(i+1)%4];
//    for(int i=0;i<n;i++) cout<<a[i]<<" ";
//    cout<<endl;
    bd(1,k+1,0);
    int cur=-1;
    for(int i=0;i<n;i++){
        if(i==(1<<(cur+1))){
            cur++;
            for(int j=i;j<(1<<cur);j++) p[pos[j]].sum2=j+1;
        }
        if(ask(1,0,(1<<k)-1,i)){
//            cout<<"!!!"<<endl;
            int u=pos[i];
//            cout<<"!"<<u<<endl;
            ok=1;
            tmp=i+1;
            upd(u);
            if(!ok) upd2(u);
        }
        if(t[i]!=-1)
            ans[t[i]]=p[1].sum1+p[1].sum2;
    }
    ll res=0;
    for(int i=0;i<m;i++) res+=ans[i]^(i+1);
    cout<<res<<endl;
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    for(int i=0;i<=20;i++) for(int j=1;j<=(1<<i);j++) tot[i]+=1ll*j;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>s[i];
        t[i]=-1;
    }
    for(int i=0;i<m;i++){
        int c;
        cin>>c;
        c--;
        t[c]=i;
    }
    for(int i=0;;i++){
        if((1<<i)>=n){
            k=i;
            break;
        }
    }
    for(int i=1;i<=k;i++){
        for(int j=1;j<(1<<(k-i));j++){
            char ch;
            cin>>ch;
            f[i][j]=ch-'0';
        }
    }
    int T;
    cin>>T;
    while(T--) slv();
    return 0;
}